#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db

class ScavSteps(db.Model):
  name = db.StringProperty(multiline=False)
  loc = db.StringProperty(multiline=False)
  clue = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)
#  next_step = db.ReferenceProperty(ScavSteps)
  hunt = db.IntegerProperty() #TODO -- all are 1
  
class ScavHunt(db.Model):
  name = db.StringProperty(multiline=False)
  description = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)
  first_step = db.ReferenceProperty(ScavSteps)
#  last_step = db.ReferenceProperty(ScavSteps)

class MainHandler(webapp.RequestHandler):

  def get(self):
    name = self.request.get('scavname')
    description = self.request.get('desc')
    clue = self.request.get('first_clue')

#     self.response.out.write("name: " + name + '<br/>')
#     self.response.out.write("desc: " + description + '<br/>')
#     self.response.out.write("first clue: " + clue + '<br/>')

    self.response.out.write("""
          <html>
            <body>
              <form action="/hunt.create" method="get">
                <div>name: <input type="text" name="scavname"></input></div>
                <div>description: <textarea name="desc" rows="3" cols="60"></textarea></div>
                <div>first clue: <textarea name="clue" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Do it"></div>
              </form>
        """)
    
    hunts = ScavHunt.all()

    for hunt in hunts:
       self.response.out.write("""
          <div style="margin-top: 10px">
            <div>Name: %s</div>
            <div>Description: %s</div>
            <div>First Clue: %s</div>
          </div>
        """ % (hunt.name, hunt.description, hunt.first_step.clue))

    self.response.out.write("""
            </body>
          </html>""")


class CreateHuntHandler(webapp.RequestHandler):

  def get(self):
    name = self.request.get('scavname')
    description = self.request.get('desc')
    clue = self.request.get('clue')

    self.response.out.write("name: " + name + '<br/>')
    self.response.out.write("desc: " + description + '<br/>')
    self.response.out.write("desc: " + clue + '<br/>')
    
    sh = ScavHunt()
    sh.name = name 
    sh.description = description

    fs = ScavSteps()

    fs.name = "First Step"
    fs.loc = "Null"
    fs.clue = clue
#    fs.next_step = "Null"

    fs.put()

    sh.first_step = fs.key()
#    sh.last_step = fs.key()
    
    sh.put()

    self.redirect("/page")

class CreateStepHandler(webapp.RequestHandler):
  def get(self):
    stepname = self.request.get('stepname')
    steploc = self.request.get('steploc')
    stepclue = self.request.get('stepclue')

    self.response.out.write("stepname: " + stepname + '<br/>')
    self.response.out.write("steploc: " + steploc + '<br/>')
    self.response.out.write("stepclue: " + stepclue + '<br/>')

    s = ScavSteps()
    
    s.name = stepname
    s.loc = steploc
    s.clue = stepclue
    s.hunt = 1 #TODO - FIX THIS!

    s.put()

    self.redirect("/step")

class StepHandler(webapp.RequestHandler):
  def get(self):
    name = self.request.get('stepname')
    loc = self.request.get('steploc')
    clue = self.request.get('stepclue')
    self.response.out.write("""
          <html>
            <body>
              <form action="/step.create" method="get">
                <div>name: <input type="text" name="stepname"></input></div>
                <div>description: <textarea name="steploc" rows="3" cols="60"></textarea></div>
                <div>clue: <textarea name="stepclue" rows="3" cols="60"></textarea></div>
                <div><input type="submit" value="Do it"></div>
              </form>
          """)

    steps = ScavSteps.all()

    steps.filter('hunt =', 1)

    steps.order('date')

    for step in steps:
      self.response.out.write("""
          <div style="margin-top: 10px">
            <div>Name: %s</div>
            <div>Location: %s</div>
            <div>Clue: %s</div>
          </div>
        """ % (step.name, step.loc, step.clue))
      self.response.out.write("""
            </body>
          </html>
      """)

def main():
  application = webapp.WSGIApplication([('/page', MainHandler),
                                        ('/step', StepHandler),
                                        ('/hunt.create', CreateHuntHandler),
                                        ('/step.create', CreateStepHandler)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
